<?php
namespace app\index\controller;

class Chat {
	 public function getChatList() {
	 	$user_id = input('session.user_id');
	 	if($user_id) {
	 		$cchat_list = model("ChatList");
	 		$data_chat_list = $cchat_list::where('uid', $user_id)->where('if_show', 1)->select();
	 		$chat_list = array();
	 		foreach ($data_chat_list as $data) {
	 			$chat = array();
	 			$chat["id"] = $data->user->id;
	 			$chat["fullname"] = $data->user->fullname;
	 			$chat["avator_path"] = $data->user->avator_path;
	 			$chat["unread"] = $data->unread;
	 			array_push($chat_list, $chat);
	 		}
	 		return json([
                'status' => 'success',
                'message' => '获取成功！',
                'data' =>  $chat_list
            ]);
	 	}
	 	else {
	 		return json([
                'status' => 'error',
                'message' => '提交错误，请重试！',
                'data' =>  null
            ]);
	 	}
	}

	public function addChatList() {
		$friend_id = input('post.friend_id');
		$user_id = input('session.user_id');
		if ($user_id and $friend_id) {
			$cchat_list = model("ChatList");
			$chat_list_data = $cchat_list::where('uid', $user_id)->where('fid', $friend_id)->find();
			if ($chat_list_data) {
				if ($chat_list_data->if_show == 0) {
					$cchat_list->save([
	                    'if_show' => 1
	                ], ['fid' => $friend_id, 'uid' => $user_id]);	                
				}
			}
			else {
				$cchat_list->data([
					'uid' => $user_id,
					'fid' => $friend_id
				]);
				$cchat_list->save();
			}
			return json([
                'status' => 'success',
                'message' => '添加成功！',
                'data' =>  null
            ]);
		}
		else {
			return json([
                'status' => 'error',
                'message' => '提交错误，请重试！',
                'data' =>  null
            ]);
		}
	}

	public function addChatContent() {
		$user_id = input('session.user_id');
		$friend_id = input('post.friend_id');
		$content = input('post.content');

		if ($user_id and $friend_id and $content) {
			$cchat_content = model('ChatContent');
			$chat_data = [
				['uid' => $user_id, 'fid' => $friend_id, 'sender' => $user_id, 'receiver' => $friend_id, 'content' => $content],
				['uid' => $friend_id, 'fid' => $user_id, 'sender' => $user_id, 'receiver' => $friend_id, 'content' => $content, 'unread' => 1]];
			$cchat_content->saveAll($chat_data);
			$this->updateUnread($friend_id, $user_id);
			return json([
                'status' => 'success',
                'message' => '添加成功！',
                'data' =>  null
            ]);
		}
		else {
			return json([
                'status' => 'error',
                'message' => '提交错误，请重试！',
                'data' =>  null
            ]);
		}
	}

	public function deleteChatList() {
		$friend_id = input('post.friend_id');
		$user_id = input('session.user_id');
		if ($user_id and $friend_id) {
			$cchat_list = model("ChatList");
			$cchat_list->save([
                'if_show' => 0
            ], ['fid' => $friend_id, 'uid' => $user_id]);
            return json([
                'status' => 'success',
                'message' => '删除成功！',
                'data' =>  null
            ]);
		}
		else {
			return json([
                'status' => 'error',
                'message' => '提交错误，请重试！',
                'data' =>  null
            ]);
		}
	}

	public function getAllUsers() {
		$user_id = input('session.user_id');
		if ($user_id) {
			$cuser = model('User');
			$user_data = $cuser::where('id', 'NEQ', $user_id)->field('id, fullname, avator_path')->select();
			return json([
	            'status' => 'success',
	            'message' => '获取成功！',
	            'data' =>  $user_data
	        ]);
		}
		else {
			return json([
                'status' => 'error',
                'message' => '提交错误，请重试！',
                'data' =>  null
            ]);
		}
	}

	public function getAllNum() {
		$user_id = input('session.user_id');
    	if (!$user_id) {
    		return json([
                'status' => 'error',
                'message' => '未登录！',
                'data' =>  null
            ]);
    	}
    	$cchat_list = model("ChatList");

    	$data_num = $cchat_list::where('uid', $user_id)->select();
    	$all_num = 0;
    	foreach ($data_num as $data) {
    		$all_num += $data->unread;
    	}
    	return json([
            'status' => 'success',
            'message' => '查询成功！',
            'data' =>  $all_num
        ]);
	}

	public function getChatContent() {
		$user_id = input('session.user_id');
		$friend_id = input('post.friend_id');
		if ($user_id and $friend_id) {
			$cchat_content = model('ChatContent');

			$data_chat_send = $cchat_content::where(function ($query) use ($user_id, $friend_id) {
			    $query->where('sender', $user_id)->where('receiver', $friend_id)->where('uid', $user_id)->where('fid', $friend_id)->where('if_show', 1);
			})->whereOr(function ($query) use ($user_id, $friend_id) {
			    $query->where('sender', $friend_id)->where('receiver', $user_id)->where('uid', $user_id)->where('fid', $friend_id)->where('if_show', 1);
			})->order('create_time')->select();

			$update_data = [];
			foreach ($data_chat_send as $data) {
				if ($data->unread != 0) {
					array_push($update_data, ['id' => $data->id, 'unread' => 0]);
				}				
			}
			$cchat_content->saveAll($update_data);
			$this->updateUnread($user_id, $friend_id);
            return json([
                'status' => 'success',
                'message' => '获取成功！',
                'data' =>  $data_chat_send
            ]);
		}
		else {
			return json([
                'status' => 'error',
                'message' => '提交错误，请重试！',
                'data' =>  null
            ]);
		}
	}

	private function updateUnread($user_id, $friend_id) {
		$cchat_list = model('ChatList');
		$cchat_content = model('ChatContent');
		$chat_data = $cchat_content::where('uid', $user_id)->where('fid', $friend_id)->select();
		$unread_num = 0;
    	foreach ($chat_data as $data) {
    		$unread_num += $data->unread;
    	}
    	$chat_list_data = $cchat_list::where('uid', $user_id)->where('fid', $friend_id)->find();
		if ($chat_list_data) {
			$cchat_list->save([
                'if_show' => 1,
                'unread' => $unread_num
            ], ['fid' => $friend_id, 'uid' => $user_id]);
		}
		else {
			$cchat_list->data([
				'uid' => $user_id,
				'fid' => $friend_id,
				'unread' => $unread_num
			]);
			$cchat_list->save();
		}
	}
}